﻿using System;
using RAP.Framework.Libary.Utils;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler
{
	/// <summary>
	/// 值转换渲染器对象
	/// </summary>
	public class ValueTypeHandler : DynamicHandler
	{

		/// <summary>
		/// 按当前节点的ValueType转换值类型
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		protected virtual object GetConvertedValue ( object value )
		{
			return this.GetConvertedValue ( value, ( this.Element as IValueTypedElement ).ValueType );
		}

		/// <summary>
		/// 按当前节点的ValueType转换值类型
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		protected virtual object GetConvertedValue ( object value, TypeCode typeCode )
		{
			if ( typeCode != TypeCode.Empty )
			{
				try
				{
					value = value.ConvertValue ( typeCode );
				}
				catch { }
			}
			return value;
		}

	}
}
